package com.mht2017.common.db;

import com.mht2017.common.api.Verifiable;
import com.mht2017.common.exception.SearchVerificationException;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;

public class DataSource implements Serializable, Verifiable {

	private static final long serialVersionUID = 1L;

	private String ip;
	private String port;
	private String sid;
	private String type;
	private String database;
	private String username;
	private String password;

	public DataSource() {
		super();
	}

	public DataSource(String ip, String port, String sid, String type, String database, String username, String password) {
		super();
		this.ip = ip;
		this.port = port;
		this.sid = sid;
		this.type = type;
		this.database = database;
		this.username = username;
		this.password = password;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (database == null ? 0 : database.hashCode());
		result = prime * result + (ip == null ? 0 : ip.hashCode());
		result = prime * result + (password == null ? 0 : password.hashCode());
		result = prime * result + (port == null ? 0 : port.hashCode());
		result = prime * result + (sid == null ? 0 : sid.hashCode());
		result = prime * result + (type == null ? 0 : type.hashCode());
		result = prime * result + (username == null ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		DataSource other = (DataSource) obj;
		if (database == null) {
			if (other.database != null) {
				return false;
			}
		} else if (!database.equals(other.database)) {
			return false;
		}
		if (ip == null) {
			if (other.ip != null) {
				return false;
			}
		} else if (!ip.equals(other.ip)) {
			return false;
		}
		if (password == null) {
			if (other.password != null) {
				return false;
			}
		} else if (!password.equals(other.password)) {
			return false;
		}
		if (port == null) {
			if (other.port != null) {
				return false;
			}
		} else if (!port.equals(other.port)) {
			return false;
		}
		if (sid == null) {
			if (other.sid != null) {
				return false;
			}
		} else if (!sid.equals(other.sid)) {
			return false;
		}
		if (type == null) {
			if (other.type != null) {
				return false;
			}
		} else if (!type.equals(other.type)) {
			return false;
		}
		if (username == null) {
			if (other.username != null) {
				return false;
			}
		} else if (!username.equals(other.username)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		String passwordString = StringUtils.center("", StringUtils.length(password), '*');
		return "DataSource [ip=" + ip + ", port=" + port + ", sid=" + sid + ", type=" + type + ", database=" + database + ", username=" + username + ", password=" + passwordString + "]";
	}

	@Override
	public void verifyWithException() throws SearchVerificationException {
		String name = "数据源校验";
		if (StringUtils.isEmpty(getType())) {
			throw new SearchVerificationException(String.format("%s-数据类类型不应该为空", name));
		}
		if (StringUtils.isEmpty(getIp())) {
			throw new SearchVerificationException(String.format("%s-ip不应该为空", name));
		}
		if (StringUtils.isEmpty(getPort())) {
			throw new SearchVerificationException(String.format("%s-port不应该为空", name));
		}
		if (StringUtils.isEmpty(getDatabase())) {
			throw new SearchVerificationException(String.format("%s-database不应该为空", name));
		}
		if (StringUtils.isEmpty(getSid())) {
			throw new SearchVerificationException(String.format("%s-sid不应该为空", name));
		}
		if (StringUtils.isEmpty(getUsername())) {
			throw new SearchVerificationException(String.format("%s-用户名不应该为空", name));
		}
		if (StringUtils.isEmpty(getPassword())) {
			throw new SearchVerificationException(String.format("%s-密码不应该为空", name));
		}
	}

}
